from django import template
# 做分页器


register = template.Library()


@register.filter()
def page_bar(page):
    page_list = []
    # 左边
    if page.number - 1 > 0:
        page_list.append(page.number - 1)
    if page.number - 2 > 0:
        page_list.append(page.number - 2)
    if page.number - 3 > 0:
        page_list.append(page.number - 3)
    if page.number - 4 > 0:
        page_list.append("···")
    if page.number != 1 and page.number != 4:
        page_list.append(1)
    page_list.reverse()
    page_list.append(page.number)
    # 右边
    if page.paginator.num_pages > page.number + 1:
        page_list.append(page.number+1)
    if page.paginator.num_pages >page.number+2:
        page_list.append(page.number+2)
    if page.paginator.num_pages > page.number+3:
        page_list.append(page.number+3)
    if page.paginator.num_pages > page.number+4:
        page_list.append('...')
    if page.paginator.num_pages != page.number:
        page_list.append(page.paginator.num_pages)
    return page_list
